package com.googlecode.rsl.tomcat;

import com.google.common.collect.Lists;
import com.googlecode.recycled.lang.jse.api.Collections;
import com.googlecode.recycled.lang.jse.api.io.CharStreamsExt;
import com.googlecode.recycled.lang.jse.api.net.Urls;
import com.googlecode.servus.content.ContentRepository;
import com.googlecode.servus.content.DownloadManager;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class Tomcats {

    public static void main(String[] args) {
        DownloadManager downloadManager = new DownloadManager(new ContentRepository<byte[]>() {
            public void save(String path, byte[] data) {
                String file = CharStreamsExt.toString(new ByteArrayInputStream(data));
                System.out.println(file);
            }

            public void delete(String path) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public boolean exists(String path) {
                return false;  //To change body of implemented methods use File | Settings | File Templates.
            }

            public byte[] load(String path) {
                return new byte[0];  //To change body of implemented methods use File | Settings | File Templates.
            }
        }, null);
        Tomcats tomcats = new Tomcats(downloadManager, Collections.<String, List<URL>>newHashMap("7.0.8", Lists.newArrayList(Urls.asUrl("file:///etc/fstabx"), Urls.asUrl("file:///etc/fstab"))));
        tomcats.download("7.0.8");
    }

    protected DownloadManager downloadManager;
    Map<String, List<URL>> urlForVersions;

    public Tomcats(DownloadManager downloadManager, Map<String, List<URL>> urlForVersions) {
        this.downloadManager = downloadManager;
        this.urlForVersions = urlForVersions;
    }

    public void download(String version) {
        for (URL url : urlForVersions.get(version)) {
            try {
                downloadManager.download(version, url);
            } catch (Exception e) {
                continue;
            }
            break;
        }
    }

}
